/*
 * @Author: cqg
 * @Date: 2020-11-25 09:42:06
 * @LastEditors: cqg
 * @LastEditTime: 2020-12-30 15:33:08
 * @Description: file content
 */
import {
	checkVoucherNoByUpdate,
	assetStorageInformation,
	addFixedWarehousing
} from "@/views/wy-asset-management/fixed-warehousing/api/fixed-warehousing.js";
import assetDictionary from "@/views/wy-asset-management/mixin/assetDictionary";
import { mapGetters } from "vuex";
import { getDict } from "~/utils";
export default {
	mixins: [assetDictionary],
	data() {
		/**
		 * async / await 同步
		 * 校验凭证号的唯一性
		 */
		let validatorVoucherNo = async (rule, value, callback) => {
			if (value == "") {
				callback(new Error("请输入凭证号"));
			}
			await checkVoucherNoByUpdate(
				value,
				this.formObj.assetFixedWarehousingId
			).then(respData => {
				let result = respData.data.data;
				if (result == false) {
					callback(new Error("凭证号重复"));
				}
			});
		};
		return {
			//操作类型
			type: this.$route.query.type,
			//基础附件信息相关
			busId: this.$route.query.assetFixedWarehousingId,
			modelType: "asset-warehousing",
			fileList: [],
			//入库附件
			wbusId: "",
			wfileList: [],
			// 是否是政府项目
			government: 0,
			//弹出框 初始时隐藏
			dialogVisible: false,

			formObj: {
				assetFixedWarehousingLogList: [],
				warehousingAttachment: "",
				cardType: "",
				cardNum: "",
				registrationPrice: "",
				registrationDate: "",
				projectId: "",
				landType: "",
				planningType: "",
				completionTime: "",
				obligee: "",
				assetCode: "",
				assetClassification: "",
				assetName: "",
				//useStatus: "",
				acquisitionMethod: "",
				acquisitionDate: "",
				investmentAcquisitionDate: "",
				valueType: "",
				registrationFee: "",
				taxation: "",
				value: "",
				number: "",
				measurementUnit: "",
				landArea: "",
				propertyRightForm: "",
				ownershipCertificateNo: "",
				issueData: "",
				ownershipPeriod: "",
				ownershipCertificate: "",
				location: "",
				ownershipNature: "",
				selfUseArea: "",
				selfUseValue: "",
				buildingStructure: "",
				depreStatus: "",
				accumDepre: "",
				estimatedLife: "",
				initDepreMonth: "",
				lastDepreMonth: "",
				salvageValue: "",
				isCard: "2",
				landNumber: "",
				landAreaCard: "",
				assetRegistrationId: "",
				area: "",
				provinceCode: "",
				cityCode: "",
				countyCode: "",
				remarks: "",
				roomIds: "",
				warehousingStatus: "", //入库状态
				warehouseRemark: ""
			},
			formObj2: {
				warehousingStatus: 1,
				assetCode2: "",
				voucherNo2: "",
				voucherDate: [],
				warehouseRemark: ""
			},
			rules2: {
				warehousingStatus: [
					{
						required: true,
						message: "请选择财务核实",
						trigger: "blur"
					}
				],
				voucherNo2: [
					{
						required: true,
						message: "请输入凭证号",
						trigger: "blur"
					},
					{ validator: validatorVoucherNo, trigger: "blur" }
				],
				voucherDate: [
					{
						required: true,
						message: "请选择凭证日期",
						trigger: "blur"
					}
				],
				warehouseRemark: [
					{
						min: 0,
						max: 100,
						message: "长度在0-100个字符串",
						trigger: "blur"
					}
				]
			}
		};
	},
	computed: {
		...mapGetters({
			cityAssembly: "getCityAssembly",
			districtCollection: "getDistrictCollection"
		})
	},
	/**
	 * 载入事件
	 */
	mounted() {
		//调用根据id获取编辑信息
		this.assetStorageInformation();
	},
	methods: {
		//选择入库状态变化
		verifyChange() {
			this.$refs.ruleForm2.clearValidate();
		},
		getDict(v) {
			return getDict(v);
		},
		async assetStorageInformation() {
			await assetStorageInformation(this.$route.query.assetFixedWarehousingId)
				.then(res => {
					this.formObj = res.data.data;
					this.government = this.formObj.assetRegistrationId == "1" ? "1" : "0";
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "数据获取失败"
					});
				})
				.finally(() => (this.loading = false));
		},

		// 是否有不动产证改变事件
		handleARealEstateCertificate() {
			this.formObj.cardType = "";
		},
		/**
		 * 保存按钮 触发文件上传
		 */
		sendData() {
			this.loading = true;
			this.$refs["ruleForm2"].validate(valid2 => {
				this.formObj.warehousingStatus = this.formObj2.warehousingStatus;
				this.formObj.assetCode = this.formObj2.assetCode2;
				this.formObj.voucherNo = this.formObj2.voucherNo2;
				this.formObj.voucherDateStart = this.formObj2.voucherDate[0];
				this.formObj.voucherDateEnd = this.formObj2.voucherDate[1];
				this.formObj.warehouseRemark = this.formObj2.warehouseRemark;
				if (valid2) this.$refs.warehousingFile.submit(this.modelType);
				else
					return this.$message({
						type: "warning",
						message: "请完整入库信息"
					});
			});
		},
		// 上传成功
		async onSuccess(response, file, fileList) {
			// let params = this.type == 'add' ? { ...this.formData, billId: response.data } : this.formData

			if (response) {
				this.formObj.warehousingAttachment = response.data;
			}

			const res = await addFixedWarehousing(this.formObj);
			if (res.data.code == 0) {
				this.$message.success(`提交成功!`);
				this.loading = false;
				//操作完成 跳转页面
				this.$router.push("/asset-management/fixed-warehousing-list");
			} else {
				this.$message.error(res.data.msg);
				this.loading = false;
			}
		}
	}
};
